﻿using System;
using System.Collections.Generic;
using System.Text;
using Rison.DataAccess.Common;
using Rison.DBHelper;

namespace Rison.DataAccess.Data.Biz
{
    public sealed class SysAttachmentDb : DataAccessBase
    {
        public SysAttachmentDb()
            : base()
        {
        }

        public SysAttachmentDb(DbBaseHelper dbTransaction)
            : base(dbTransaction)
        {
        }

        /// <summary>
        /// 构造函数。
        /// </summary>
        /// <param name="tablename">保存附件的数据表名称。</param>
        public SysAttachmentDb(string tablename)
            : base()
        {
            _tablename = tablename;
        }


        /// <summary>
        /// 构造函数。
        /// </summary>
        /// <param name="tablename">保存附件的数据表名称。</param>
        /// <param name="dbTransaction">数据库事务对象。</param>
        public SysAttachmentDb(string tablename, DbBaseHelper dbTransaction)
            : base(dbTransaction)
        {
            _tablename = tablename;
        }

        /// <summary>
        /// 构造函数。
        /// </summary>
        /// <param name="tablename">保存附件的数据表名称。</param>
        /// <param name="parentKeyname">附件表中，附件所属的数据对象的字段名称。</param>
        public SysAttachmentDb(string tablename, string parentKeyname)
            : base()
        {
            _tablename = tablename;
            _parentKeyname = parentKeyname;
        }

        /// <summary>
        /// 构造函数。
        /// </summary>
        /// <param name="tablename">保存附件的数据表名称。</param>
        /// <param name="parentKeyname">附件表中，附件所属的数据对象的字段名称。</param>
        /// <param name="dbTransaction">数据库事务对象。</param>
        public SysAttachmentDb(string tablename, string parentKeyname, DbBaseHelper dbTransaction)
            : base(dbTransaction)
        {
            _tablename = tablename;
            _parentKeyname = parentKeyname;
        }


        private string _tablename = string.Empty;
        private string _parentKeyname = string.Empty;

        /// <summary>
        /// 保存附件的数据表中，附件所属的主对象的字段名称。
        /// </summary>
        private string parentKeyname
        {
            get { return string.IsNullOrEmpty(_parentKeyname) ? "ParentId" : _parentKeyname; }
        }

        /// <summary>
        /// 保存附件的数据表名。
        /// </summary>
        protected override string tableName
        {
            get { return string.IsNullOrEmpty(_tablename) ? "Sys_Attachment" : _tablename; }
        }

        protected override string IdentityFieldName
        {
            get { return "FileId"; }
        }

        /// <summary>
        /// 根据附件所属的数据对象的主键值，删除所有附件。
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public int DeleteAttachmentByParent(long parentId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("delete from {0} where {1}={2}", tableName, parentKeyname, parentId.ToString());

            return base.ExecuteNonSql(sql.ToString());
        }
    }
}
